package com.baizhi.controller;


import com.baizhi.entity.CmfzAdmin;
import com.baizhi.service.AdminService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("login")
    public  Map  login(CmfzAdmin cmfzAdmin, String validateCode){
        Map map = new HashMap<>();
//        1.将页面用户输入的账号的密码  封装到token中
        UsernamePasswordToken token = new UsernamePasswordToken(cmfzAdmin.getUsername(),cmfzAdmin.getPassword());
//        2.获取subject
        Subject subject = SecurityUtils.getSubject();

//        3.使用subject的认证方法完成认证
        Session session = subject.getSession();
        String reallCode =(String) session.getAttribute("code");
        System.out.println("用户输入的："+validateCode+"   真实的验证码："+reallCode);
        //判断验证码是否正确
//        if(!(reallCode!=null&&reallCode.equalsIgnoreCase(validateCode))) {
//            String errorMsg = "验证码不正确！！！";
//            map.put("errMsg",errorMsg);
//        }
        try {
            session.setAttribute("admin", cmfzAdmin);
            subject.login(token);
            System.out.println("认证成功");
            map.put("flag",true);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("register")
    public Map adminRester(CmfzAdmin cmfzAdmin){
        Map map = new HashMap<>();
        try {
            adminService.insert(cmfzAdmin);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }

        return map;

    }


/*    @RequestMapping("login")
    @ResponseBody
    public  Map  login(CmfzAdmin admin, String validateCode, HttpSession session, HttpServletResponse resp){
        System.out.println("username="+admin.getUsername());
        System.out.println("password="+admin.getPassword());
        Map map = new HashMap<>();
        boolean result = adminService.login(admin.getUsername(),admin.getPassword());
        String reallCode =(String) session.getAttribute("code");
        System.out.println("用户输入的："+validateCode+"   真实的验证码："+reallCode);
        //判断验证码是否正确
        if(!(reallCode!=null&&reallCode.equalsIgnoreCase(validateCode))) {
            String errorMsg = "验证码不正确！！！";
            map.put("errMsg",errorMsg);
            return map;
        }
        if(result) {
            //登录成功、创建登录标记、存入session作用域
            session.setAttribute("admin", true);
            //1、创建cookie对象并传入参数
            //2、设置cookie的存活时间
            //3、将kookie响应给浏览器
            Cookie cookie = new Cookie("username",admin.getUsername() );
            //cookie.setMaxAge(60*60*24);
            resp.addCookie(cookie);
            map.put("flag",true);
            return map;
        }else{
        map.put("flag",false);
        return map;

        }
    }*/

    @RequestMapping("selectAll")
    public Map selectAll(Integer page, Integer limit){
        Page<CmfzAdmin> cmfzAdminPage = adminService.selectAll(page, limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg",0);
        map.put("count",cmfzAdminPage.getTotal());
        map.put("data",cmfzAdminPage.getRecords());
        return map;
    }

    @RequestMapping("insert")
    public  Map   insert(CmfzAdmin cmfzAdmin){
        Map map = new HashMap<>();
        try {
            adminService.insert(cmfzAdmin);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
}
